#include "VerifyGrpcClient.hpp"
#include "RpcConnectPool.hpp"
#include "const.hpp"
#include "message.grpc.pb.h"
#include "message.pb.h"
#include <grpcpp/channel.h>
#include <grpcpp/client_context.h>
#include <grpcpp/create_channel.h>
#include <grpcpp/security/credentials.h>
#include <grpcpp/support/status.h>
#include <memory>
#include <string>

VerifyGrpcClient::VerifyGrpcClient() {
  std::string Host = config.get<std::string>("VerifyServer.Host");
  std::string Port = config.get<std::string>("VerifyServer.Port");
  _pool =
      std::make_unique<RpcConnectPool<message::VerifyService>>(4, Host, Port);
  std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(
      Host + ":" + Port, grpc::InsecureChannelCredentials());
};
message::GetVerifyRsp VerifyGrpcClient::GetVerifyCode(std::string &email) {
  grpc::ClientContext context;
  message::GetVerifyRsp reply;
  message::GetVerifyReq request;
  request.set_email(email);
  auto stub = _pool->GetConnection();
  grpc::Status status = stub->GetVerifyCode(&context, request, &reply);
  if (!status.ok()) {
    reply.set_error(ErrorCodes::RPCFailed);
  }
  _pool->returnConnection(std::move(stub));
  return reply;
}